import axios from 'axios'
import graphfactory from '../graphdatafactory'
var factory = graphfactory.GraphDataFactory;

class TSymbolDef {
    constructor() {
        this.uuid = '';
        this.name = "";
        this.type = 0;
        this.iconURL = "/res/icon/unknown.png";
        this.category = "other";
        this.unitFixedFlag = 1;

        //0-常规，模具中图元全部作为整体，不可单独编辑
        //1-容器，模具作为容器，一般有一个标题，其中图元可单独调整宽高，标题可单独编辑
        //2-容器子项，模具作为容器中子项，如类成员、树节点、列表子项等，可单独调整宽高和编辑文本，放入容器模具中，会自动吸附及调整宽度跟容器对齐
        //3-软件包
        this.moldActionType = 0;
        this.autoLabel = true;
        this.dlftLabPos = "center";//default label position describtion
        this.state = 0;
    }
}

var SymbolDAO = {

}

SymbolDAO.postRqstSymbol = function (onNotifySymbolLoadFinish) {
    let that = this;
    //先获取系统模具
    let symRet = null;
    var url = "api/symbol/getSystemSymol";
    var postObj = {};
    axios.post(url, postObj).then(res => {
        //解析从后台返回的系统模具json数据，是一个json对象，data元素为所有图符图元组成的长字符串
        let symbolArr = res.data;
        symRet = this.parsingSymbol(symbolArr,onNotifySymbolLoadFinish);
        
    }).catch(function (error) {
        console.log(error);
    })
    return symRet;
}


SymbolDAO.postRqstSymbolByCategory = function (className,onNotifySymbolLoadFinish) {
    var url = "api/symbol/getSymbolByClass";
    var postObj = { "category": className, "userid": "longx13" };

    axios.post(url, postObj).then(res => {
        //解析从后台返回的系统模具json数据，是一个json对象，data元素为所有图符图元组成的长字符串
        let symbolArr = res.data;
        let symRet = this.parsingSymbol(symbolArr,onNotifySymbolLoadFinish);
        if (symRet && onNotifySymbolLoadFinish){
            symRet["categoryList"] = [className];
            onNotifySymbolLoadFinish(symRet);
        }
        
    }).catch(function (error) {
        console.log(error);
    })
    return ;
}

SymbolDAO.parsingSymbol = function (symbolArr) {
    //解析图符数据字符串
    let tAllSymbolUnitLst = new Array();
    let tAllSymbolDefLst = new Array();
    for (let i = 0; i < symbolArr.length; i++) {
        let symbol = symbolArr[i];
        var obj = JSON.parse(symbol["data"]);
        var jsonUnitlst = obj["elements"];
        let tSymbolDef = new TSymbolDef();
        tSymbolDef.uuid = symbol['uuid'];
        tSymbolDef.name = symbol["name"];
        tSymbolDef.type = symbol["typeID"];
        tSymbolDef.iconURL = symbol["iconURL"];
        tSymbolDef.category = symbol["category"];
        tSymbolDef.unitFixedFlag = symbol["unitFixedFlag"];
        tSymbolDef.moldActionType = symbol["moldActionType"];
        tSymbolDef.autoLabel = symbol["autoLabel"];
        tSymbolDef.dlftLabPos = symbol["dlftLabPos"];

        tAllSymbolDefLst.push(tSymbolDef);
        this.parsingSymbolUnitJsonArr(jsonUnitlst, tAllSymbolUnitLst,tSymbolDef.uuid,tSymbolDef.type);
    }

    //获取完系统模具，再获取当前登录用户的自定义模具
    // this.post2OpenCustSymbol(tAllSymbolUnitLst,tAllSymbolDefLst);
    let symRet = {unitArr:tAllSymbolUnitLst,symbolArr:tAllSymbolDefLst};
    return symRet;
}


SymbolDAO.post2OpenCustSymbol = function (tAllSymbolUnitLst,tAllSymbolDefLst,onNotifySymbolLoadFinish) {
    var url = "api/symbol/getCustSymbol";
    var postObj = {userid:"longx13"};
    let that = this;
    axios.post(url, postObj).then(res => {
        that.parsingCustSymbolArray(tAllSymbolUnitLst,tAllSymbolDefLst, res.data);
        if (onNotifySymbolLoadFinish){
            let symRet = {unitArr:tAllSymbolUnitLst,symbolArr:tAllSymbolDefLst};
            onNotifySymbolLoadFinish(symRet);
        }
    }).catch(function (error) {
        console.log(error);
    })
}

SymbolDAO.parsingCustSymbolArray = function (tAllSymbolUnitLst,tAllSymbolDefLst, symbolArr) {
    var objList = this.parsingCustSymbolFileData(symbolArr,tAllSymbolDefLst);
    let len = objList.length;
    for (let i = 0; i < len; i++) {
        let obj = objList[i];
        tAllSymbolUnitLst.push(obj);
    }
}

SymbolDAO.parsingCustSymbolFileData = function (symbolArr,tAllSymbolDefLst) {
    let unitInfoLst = new Array();
    for (var i = 0; i < symbolArr.length; i++) {
        var symbol = symbolArr[i];
        var obj = JSON.parse(symbol["data"]);
        var jsonUnitlst = obj["elements"];

        let tSymbolDef = new TSymbolDef();
        tSymbolDef.uuid = symbol['uuid'];
        tSymbolDef.name = symbol["name"];
        tSymbolDef.type = symbol["typeID"];
        tSymbolDef.iconURL = symbol["iconURL"];
        tSymbolDef.category = symbol["category"];
        tSymbolDef.unitFixedFlag = symbol["unitFixedFlag"];
        tSymbolDef.moldActionType = symbol["moldActionType"];
        
        tSymbolDef.autoLabel = symbol["autoLabel"];
        tSymbolDef.dlftLabPos = symbol["dlftLabPos"];
        tAllSymbolDefLst.push(tSymbolDef);
        this.parsingSymbolUnitJsonArr(jsonUnitlst, unitInfoLst,tSymbolDef.uuid,tSymbolDef.type);
    }
    return unitInfoLst;
}

SymbolDAO.parsingSymbolUnitJsonArr = function (jsonUnitlst, unitInfoLst,symbolUUID,symbolType) {
    for (var i = 0; i < jsonUnitlst.length; i++) {
        var unitObj = jsonUnitlst[i];
        var id = unitObj["type"];
        if (id == null){
            continue;
        }
        var strID = id.toString(16);
        var devFullType = unitObj["type"];
        var graphInfo = factory.create(devFullType);
        if (graphInfo != null) {
            graphInfo.fromJson(unitObj);
            graphInfo.superID = symbolUUID;
            graphInfo.symbolType = symbolType;
            unitInfoLst.push(graphInfo);
        }
    }
    return unitInfoLst;
}

SymbolDAO.initBaseUnit = function(onNotifySymbolLoadFinish)
{
    let tAllSymbolUnitLst = [];
    let tAllSymbolDefLst = [];
    this.getAllBaseUnit(tAllSymbolDefLst);
    let symRet = {categoryList:['base-unit'],unitArr:tAllSymbolUnitLst,symbolArr:tAllSymbolDefLst};
    if (onNotifySymbolLoadFinish){
        onNotifySymbolLoadFinish(symRet);
    }
    return symRet;
}

SymbolDAO.getAllBaseUnit = function(tAllSymbolDefLst) {
    let unit = { type: 0x030a0003, name: '连接线', iconURL: "/res/icon/straightline.png", 
    category: "base-unit",tips: '连接线。智能避让。鼠标松开结束绘制' };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0X030a0004, name: '多段连接线', iconURL: '/res/icon/polyline.png',
     category: "base-unit" ,tips: '鼠标左键按下增加折点，右键按下结束绘制'};
    tAllSymbolDefLst.push(unit);

    unit = { type: 0X02070001, name: '母线', iconURL: '/res/icon/bus.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0X03130000, name: '样条曲线', iconURL: '/res/icon/spline.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0X030B0000, name: '矩形', iconURL: '/res/icon/rect.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0X030C0000, name: '圆形', iconURL: '/res/icon/circle.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0X03230000, name: '椭圆', iconURL: '/res/icon/ellipse.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    //正多边形
    unit = { type: 0x030d0103, name: '三角形', iconURL: '/res/icon/0x030d0103.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0104, name: '正方形', iconURL: '/res/icon/0x030d0104.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0105, name: '五边形', iconURL: '/res/icon/0x030d0105.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0106, name: '六边形', iconURL: '/res/icon/0x030d0106.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0107, name: '七边形', iconURL: '/res/icon/0x030d0107.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0108, name: '八边形', iconURL: '/res/icon/0x030d0108.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0109, name: '九边形', iconURL: '/res/icon/0x030d0109.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d010a, name: '十边形', iconURL: '/res/icon/0x030d010a.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    //多角星形

    unit = { type: 0x030d0203, name: '三角星', iconURL: '/res/icon/0x030d0203.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0204, name: '四角星', iconURL: '/res/icon/0x030d0204.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0205, name: '五角星', iconURL: '/res/icon/0x030d0205.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0206, name: '六角星', iconURL: '/res/icon/0x030d0206.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0207, name: '七角星', iconURL: '/res/icon/0x030d0207.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0208, name: '八角星', iconURL: '/res/icon/0x030d0208.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0209, name: '九角星', iconURL: '/res/icon/0x030d0209.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d020a, name: '十角星', iconURL: '/res/icon/0x030d020a.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0210, name: '十六角星', iconURL: '/res/icon/0x030d0210.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0218, name: '二十四角星', iconURL: '/res/icon/0x030d0218.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0220, name: '三十二角星', iconURL: '/res/icon/0x030d0220.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x030d0240, name: '六十四角星', iconURL: '/res/icon/0x030d0240.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03320001, name: '圆柱', iconURL: '/res/icon/0x03320001.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03330001, name: '立方体', iconURL: '/res/icon/0x03330001.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03341111, name: '直切角矩形', iconURL: '/res/icon/0x03341111.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03342222, name: '圆切角矩形', iconURL: '/res/icon/0x03342222.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03340001, name: '剪去单角矩形', iconURL: '/res/icon/0x03340001.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03341001, name: '剪去顶部两角矩形', iconURL: '/res/icon/0x03341001.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03340110, name: '剪去底部两角矩形', iconURL: '/res/icon/0x03340110.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03341010, name: '剪去对角矩形1', iconURL: '/res/icon/0x03341010.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03340101, name: '剪去对角矩形2', iconURL: '/res/icon/0x03340101.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03340002, name: '右上圆角矩形', iconURL: '/res/icon/0x03340002.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03342002, name: '顶部两个圆角矩形', iconURL: '/res/icon/0x03342002.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);
    unit = { type: 0x03340220, name: '底部两个圆角矩形', iconURL: '/res/icon/0x03340220.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03342020, name: '对角圆角矩形1', iconURL: '/res/icon/0x03342020.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03340202, name: '对角圆角矩形2', iconURL: '/res/icon/0x03340202.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03341212, name: '剪去直角和圆角的矩形1', iconURL: '/res/icon/0x03341212.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    unit = { type: 0x03342121, name: '剪去直角和圆角的矩形2', iconURL: '/res/icon/0x03342121.png', category: "base-unit" };
    tAllSymbolDefLst.push(unit);

    // unit = { type: 0x02070001, name: '通信线', iconURL: '/res/icon/comline.png', category: "base-unit" };
    // tAllSymbolDefLst.push(unit);



    // unit = { type: 0X03100000, name: '二次贝塞尔曲线', iconURL: '/res/icon/quadraticcurve.png', category: "base-unit" };
    // tAllSymbolDefLst.push(unit);

    // unit = { type: 0X03110000, name: '三次贝塞尔曲线', iconURL: '/res/icon/beziercurve.png', category: "base-unit" };
    // tAllSymbolDefLst.push(unit);

    // unit = { type: 0X03120000, name: '辅助线', iconURL: '/res/icon/auxline.png', category: "base-unit" };
    // tAllSymbolDefLst.push(unit);

    // unit = { type: 0X03240000, name: 'image', iconURL: '/res/icon/image.png', category: "base-unit" };
    // tAllSymbolDefLst.push(unit);
}

export default { SymbolDAO ,TSymbolDef}